package com.breeze.components.task.xxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.breeze.components.core.exception.CommonException;
import com.breeze.components.task.xxl.config.properties.XxlJobProperties;
import com.breeze.components.task.xxl.model.XxlJobInfo;
import com.breeze.components.task.xxl.service.JobInfoService;
import com.breeze.components.task.xxl.service.JobLoginService;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 任务信息操作实现类
 *
 * @author breeze
 */
@Service
@Slf4j
public class JobInfoServiceImpl implements JobInfoService {

  @Resource XxlJobProperties properties;

  @Resource private JobLoginService jobLoginService;

  @Override
  public List<XxlJobInfo> getJobInfo(Integer jobGroupId, String executorHandler) {
    try {
      String url = properties.getAdminAddresses() + "/jobinfo/pageList";
      HttpResponse response =
          HttpRequest.post(url)
              .form("jobGroup", jobGroupId)
              .form("executorHandler", executorHandler)
              .form("triggerStatus", -1)
              .cookie(jobLoginService.getCookie())
              .execute();

      JSONArray array = JSON.parseObject(response.body()).getJSONArray("data");
      return array.toJavaList(XxlJobInfo.class);
    } catch (Exception ex) {
      throw new CommonException("获取jobInfo失败");
    }
  }

  @Override
  public String addJobInfo(XxlJobInfo xxlJobInfo) {
    try {
      String url = properties.getAdminAddresses() + "/jobinfo/add";
      Map<String, Object> paramMap = BeanUtil.beanToMap(xxlJobInfo);
      HttpResponse response =
          HttpRequest.post(url).form(paramMap).cookie(jobLoginService.getCookie()).execute();

      JSONObject json = JSON.parseObject(response.body());
      Integer code = json.getInteger("code");
      if (Objects.equals(code, 200)) {
        return json.getString("content");
      }
      log.error("add jobInfo error! massage[{}]", json.getString("msg"));
      return null;
    } catch (Exception ex) {
      throw new CommonException("添加xxlJobInfo失败");
    }
  }

  @Override
  public Boolean updateJobInfo(XxlJobInfo xxlJobInfo) {
    try {
      String url = properties.getAdminAddresses() + "/jobinfo/update";
      Map<String, Object> paramMap = BeanUtil.beanToMap(xxlJobInfo);
      HttpResponse response =
          HttpRequest.post(url).form(paramMap).cookie(jobLoginService.getCookie()).execute();
      JSONObject json = JSON.parseObject(response.body());
      Integer code = json.getInteger("code");
      if (Objects.equals(code, 200)) {
        return true;
      }
      log.error("update jobInfo error! message[{}]", json.getString("msg"));
      return false;
    } catch (Exception ex) {
      throw new CommonException("更新xxlJobInfo失败");
    }
  }
}
